package view.frontend.order;

import exceptionManager.ExceptionManager;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import model.GraveYard;
import view.ViewHandler;
import view.ViewSingleton;
import view.generaldesign.DesignLayout;
import view.menu.MenuLabel;

/*
 @author Mathias and Sonny Sandberg
 */
public class OrderGraveYard {

    private JPanel 
            graveYardInfoPanel,
            newGraveYardPanel,
            existingGraveYardsPanel,
            scrollPanel;
    private JTextField tFieldGraveYard,
            tFieldArea,
            tFieldRow,
            tFieldNumber,
            tFieldCommission;
    private JRadioButton radioUrn,
            radioCoffin;
    private Order order;
    private JScrollPane scrollPane;

    public OrderGraveYard(Order view) {
        order = view;

        newGraveYardPanel = new JPanel(null);
        newGraveYardPanel.setBounds(0, 0, 300, 455);
        newGraveYardPanel.setOpaque(false);
        
        scrollPane = new JScrollPane();
        
        buildNewGraveYardPanel();
        buildExistingGraveYardPanel();
        
        newGraveYardPanel.setVisible(false);
    }
    
    private void buildNewGraveYardPanel()
    {
        graveYardInfoPanel = new JPanel(null);
        graveYardInfoPanel.setBounds(0, 0, 300, 225);
        graveYardInfoPanel.setOpaque(false);
        graveYardInfoPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Gravstedsinfo", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        
        JLabel lblGraveYard = new JLabel("Kirkegård");
        lblGraveYard.setBounds(10, 50, 280, 18);
        lblGraveYard.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));

        JLabel lblArea = new JLabel("Afdeling");
        lblArea.setBounds(10, 100, 280, 18);
        lblArea.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));

        JLabel lblRow = new JLabel("Række");
        lblRow.setBounds(10, 155, 75, 18);
        lblRow.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));

        JLabel lblNumber = new JLabel("Nr.");
        lblNumber.setBounds(205, 155, 30, 18);
        lblNumber.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        JLabel lblMorticianCommission = new JLabel("Bedemands Provision");
        lblMorticianCommission.setBounds(10, 180, 240, 25);
        lblMorticianCommission.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        JLabel lblMorticianCommissionPercentage = new JLabel("%", JLabel.CENTER);
        lblMorticianCommissionPercentage.setBounds(270, 180, 20, 25);
        lblMorticianCommissionPercentage.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        radioUrn = new JRadioButton("Urne");
        radioUrn.setBounds(10, 25, 140, 20);
        radioUrn.setOpaque(false);
        radioUrn.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        radioUrn.setSelected(true);
        radioUrn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                order.getOrderObject().getTombStone().setCuffin(false);
            }
        });

        radioCoffin = new JRadioButton("Kiste");
        radioCoffin.setBounds(150, 25, 140, 20);
        radioCoffin.setOpaque(false);
        radioCoffin.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        radioCoffin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                order.getOrderObject().getTombStone().setCuffin(true);
            }
        });

        ButtonGroup group = new ButtonGroup();
        group.add(radioUrn);
        group.add(radioCoffin);
        
        tFieldGraveYard = new JTextField("");
        tFieldGraveYard.setBounds(10, 70, 280, 25);
        tFieldGraveYard.setBorder(DesignLayout.T_FIELD_RED);
        tFieldGraveYard.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldGraveYard.setFont(DesignLayout.STANDARD);
        tFieldGraveYard.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                existingGraveYardsPanel.setVisible(true);
                order.getOrderObject().getTombStone().getGraveYard().reset();
                doLookUp(tFieldGraveYard.getText());
            }
        });

        tFieldArea = new JTextField("");
        tFieldArea.setBounds(10, 120, 280, 25);
        tFieldArea.setBorder(DesignLayout.T_FIELD_RED);
        tFieldArea.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldArea.setFont(DesignLayout.STANDARD);
        tFieldArea.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                order.getOrderObject().getTombStone().setArea(tFieldArea.getText());
            }
        });

        tFieldRow = new JTextField("");
        tFieldRow.setBounds(70, 152, 50, 25);
        tFieldRow.setBorder(DesignLayout.T_FIELD_RED);
        tFieldRow.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldRow.setFont(DesignLayout.STANDARD);
        tFieldRow.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldRow.addFocusListener(new FocusListener() 
        {
            @Override
            public void focusGained(FocusEvent e)
            {
                tFieldRow.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e)
            {
                tFieldRow.select(0, 0);
            }
        });
        tFieldRow.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                try
                {
                    order.getOrderObject().getTombStone().setTheRow(Integer.parseInt(tFieldRow.getText()));
                } 
                catch (NumberFormatException ex)
                {
                    tFieldRow.setText(""+order.getOrderObject().getTombStone().getTheRow());
                }
            }
        });

        tFieldNumber = new JTextField("");
        tFieldNumber.setBounds(240, 152, 50, 25);
        tFieldNumber.setBorder(DesignLayout.T_FIELD_RED);
        tFieldNumber.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldNumber.setFont(DesignLayout.STANDARD);
        tFieldNumber.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldNumber.addFocusListener(new FocusListener() 
        {
            @Override
            public void focusGained(FocusEvent e)
            {
                tFieldNumber.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e)
            {
                tFieldNumber.select(0, 0);
            }
        });
        tFieldNumber.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                try
                {
                    order.getOrderObject().getTombStone().setTheNumber(Integer.parseInt(tFieldNumber.getText()));
                } 
                catch (NumberFormatException ex)
                {
                    tFieldNumber.setText(""+order.getOrderObject().getTombStone().getTheNumber());
                }
            }
        });

        tFieldCommission = new JTextField(""+order.getOrderObject().getTombStone().getCommision());
        tFieldCommission.setBounds(215, 180, 50, 25);
        tFieldCommission.setBorder(DesignLayout.T_FIELD_RED);
        tFieldCommission.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldCommission.setFont(DesignLayout.STANDARD);
        tFieldCommission.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldCommission.addFocusListener(new FocusListener() 
        {
            @Override
            public void focusGained(FocusEvent e)
            {
                tFieldCommission.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e)
            {
                tFieldCommission.select(0, 0);
            }
        });
        tFieldCommission.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                try
                {
                    order.getOrderObject().getTombStone().setCommision(Double.parseDouble(tFieldCommission.getText()));
                } 
                catch (NumberFormatException ex)
                {
                    tFieldCommission.setText(""+order.getOrderObject().getTombStone().getCommision());
                }                
            }
        });

        graveYardInfoPanel.add(lblGraveYard);
        graveYardInfoPanel.add(lblArea);
        graveYardInfoPanel.add(lblRow);
        graveYardInfoPanel.add(lblNumber);
        graveYardInfoPanel.add(lblMorticianCommission);
        graveYardInfoPanel.add(lblMorticianCommissionPercentage);
        graveYardInfoPanel.add(radioUrn);
        graveYardInfoPanel.add(radioCoffin);
        graveYardInfoPanel.add(tFieldGraveYard);
        graveYardInfoPanel.add(tFieldArea);
        graveYardInfoPanel.add(tFieldRow);
        graveYardInfoPanel.add(tFieldNumber);
        graveYardInfoPanel.add(tFieldCommission);

        graveYardInfoPanel.setVisible(true);
        newGraveYardPanel.add(graveYardInfoPanel);        
    }
    
    public void setFieldsFromModel()
    {
        model.TombStone tombObject = order.getOrderObject().getTombStone();
        
        if (tombObject.isCuffin() == true)
        {
            radioUrn.setSelected(false);
            radioCoffin.setSelected(true);
        }
        else
        {
            radioUrn.setSelected(true);
            radioCoffin.setSelected(false);
        }
        
        if (tombObject.getGraveYard() != null)
        {
            tFieldGraveYard.setText(tombObject.getGraveYard().getName());
        }
        
        tFieldArea.setText(tombObject.getArea());
        tFieldRow.setText(""+tombObject.getTheRow());
        tFieldNumber.setText(""+tombObject.getTheNumber());
        tFieldCommission.setText(""+tombObject.getCommision());
    }

    private void buildExistingGraveYardPanel()
    {
        existingGraveYardsPanel = new JPanel(null);
        existingGraveYardsPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Vælg Kirkegård", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        existingGraveYardsPanel.setBounds(0, 235, 300, 220);
        existingGraveYardsPanel.setOpaque(false);
        
        scrollPanel = new JPanel(null);
        scrollPanel.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        scrollPanel.setBackground(Color.white);
        scrollPanel.setBorder(null);
        
        scrollPane.setBorder(DesignLayout.T_FIELD_RED);
        scrollPane.setBounds(20, 34, 260, 170);
        scrollPane.setViewportView(scrollPanel);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setUnitIncrement(15);
        
        existingGraveYardsPanel.add(scrollPane);
        existingGraveYardsPanel.setVisible(false);
        newGraveYardPanel.add(existingGraveYardsPanel);
    }
    
    public void buildFoundPanel(GraveYard[] graveYard)
    {
        scrollPanel.removeAll();
        
        for (int i = 0; i < graveYard.length; i++)
        {
            OrderGraveYardListPanel panel = new OrderGraveYardListPanel(this, graveYard[i]);
            panel.setBounds(10, 10 + (i * 140), 220, 130);
            scrollPanel.setPreferredSize(new Dimension(220, 150 + (i * 140)));
            scrollPanel.add(panel);
        }
        
        if (graveYard.length == 0)
        {
            JPanel panel = new JPanel(null);
            panel.setOpaque(false);
            panel.setBounds(10, 10, 220, 130);
            scrollPanel.add(panel);
            
            JLabel label = new JLabel("<html><div style='text-align:center; width: 170px;'><b>Ingen Resultater</b><br /><i>Klik Her For At Oprette Kirkegård</i></div></html>");
            label.setBounds(0, 0, 220, 130);
            label.setOpaque(true);
            label.setBorder(DesignLayout.T_FIELD_RED);
            label.setBackground(DesignLayout.T_FIELD_BG_COLOR);
            label.addMouseListener(new MouseAdapter()
            {
                @Override
                public void mouseReleased(MouseEvent e)
                {
                    ViewHandler.toggleAllsView("orderContainer", false);
                    ViewHandler.toggleAllsView("menuLeft", true);
                    ViewHandler.setPageTitle("Administrér Kirkegård");
                    ViewHandler.toggleAllsView("adminGraveYard", true);
                    
                    // Reset top menu background colors
                    view.ViewSingleton.getSingletonObject().getMenus()[0].resetBackgroundColor(DesignLayout.T_FIELD_BORDER_RED);
                    
                    // Now change the ADMIN menu to selected color
                    ArrayList<MenuLabel> menuLabels = ViewSingleton.getSingletonObject().getMenuLabels();
                    for (MenuLabel value : menuLabels)
                    {
                        if (value.getMenuLabelText().equals("ADMIN"))
                        {
                            value.setBackgroundColor();
                        }
                    }
                }
            });
            
            panel.add(label);
        }
        
        scrollPanel.revalidate();
        scrollPanel.repaint();       
    }
    
    private void doLookUp(String searchFor)
    {
        try
        {
            GraveYard[] graveYards = model.handlers.GraveYardHandler.getGraveYardArrayByString(searchFor);
            buildFoundPanel(graveYards);
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
    }

    public JPanel getExistingGraveYardsPanel()
    {
        return existingGraveYardsPanel;
    }

    public JTextField gettFieldGraveYard()
    {
        return tFieldGraveYard;
    }

    public Order getOrder()
    {
        return order;
    }

    public JPanel getPanel() {
        return newGraveYardPanel;
    }
}